package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"turn-around/internal/models/dao"
	"turn-around/internal/models/services"
	"turn-around/internal/request"
)

func AddGoods(c *gin.Context) {
	var req request.Goods
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	goods := dao.Goods{
		Name:     req.Name,
		Colour:   req.Colour,
		Capacity: req.Capacity,
		Channel:  req.Channel,
		Battery:  req.Battery,
		Status:   req.Status,
		Price:    req.Price,
	}
	err = services.CreateGoods(goods)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "添加商品失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "添加商品成功",
		})
	}
}
